home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / newmarch.zip / FONT.C < prev    next >
C/C++ Source or Header  |  1992-09-08  |  6KB  |  242 lines

  1. /* Author:   $Author: jan $
  2.  * File:     $Source: /usr/usrs/jan/desktop/X_Book.boo/programs/RCS/font.c,v $
  3.  * Date:     $Date: 1992/09/09 00:09:52 $
  4.  * Revision: $Revision: 1.1 $
  5.  */
  6.  
  7. #include "copyright.h"
  8.  
  9. /*     
  10. ** File: onewindow.c
  11. ** Generic program schema for a one window program    
  12. */ 
  13.     
  14. #include <stdio.h>    
  15. #include <X11/Xlib.h>    
  16. #include <X11/Xutil.h>    
  17.     
  18. /*    
  19. ** debug lists all events to an 
  20. ** xterm window as they occur
  21. */    
  22. #define DEBUG    
  23.     
  24. char WINDOW_NAME[] = "Window";    
  25. char ICON_NAME[] = "Icon";    
  26.     
  27. Display *display;  /* the display device */    
  28. int     screen;    /* the screen on the display */    
  29. Window  main_window;    
  30. GC    gc;    
  31. unsigned long  foreground, background;    
  32.  
  33. char FONT[] = "9x15";
  34.  
  35. Font    font;
  36.     
  37.  
  38.     
  39. /*    
  40. ** 
  41. */    
  42.     
  43. Window    
  44. openWindow (x, y, width, height, border_width,
  45.             argc, argv)    
  46.     int x, y;  /* coords of the upper left 
  47.                   corner in pixels */
  48.     int width,    
  49.         height;  /* size of the window in pixels */    
  50.     int border_width;  /* the border width is 
  51.                           not included in the    
  52.                           other dimensions */    
  53.     int argc;    
  54.     char **argv;    
  55. {    
  56.     Window  new_window;    
  57.     XSizeHints  size_hints;    
  58.         
  59.     /* now create the window */    
  60.     new_window = XCreateSimpleWindow (display,     
  61.                         DefaultRootWindow (display),    
  62.                         x, y, width, height,    
  63.                         border_width,    
  64.                         foreground, background);    
  65.             
  66.     /* set up the size hints for the window manager */
  67.     size_hints.x = x;    
  68.     size_hints.y = y;    
  69.     size_hints.width = width;    
  70.     size_hints.height = height;    
  71.     /* and state what hints are included */    
  72.     size_hints.flags = PPosition | PSize;    
  73.     
  74.     /* let the window manager know about the window */
  75.     XSetStandardProperties (display, new_window,    
  76.           WINDOW_NAME, ICON_NAME,    
  77.           None,      /* no icon map */    
  78.           argv, argc, &size_hints);    
  79.     
  80.     /* Decide what events the window will receive */
  81.     XSelectInput (display, new_window,    
  82.           (ButtonPressMask | KeyPressMask | 
  83.            ExposureMask));    
  84.     
  85.     /* Return the window ID */    
  86.     return (new_window);    
  87. }    
  88.     
  89. /*    
  90. ** 
  91. */    
  92. GC   
  93. getGC ()    
  94. {    
  95.     GC gc;    
  96.     XGCValues gcValues;    
  97.     
  98.     gc = XCreateGC (display, main_window,     
  99.                 (unsigned long) 0, &gcValues);    
  100.     
  101.     XSetBackground (display, gc, background);    
  102.     XSetForeground (display, gc, foreground);    
  103.     if (font != 0)
  104.         XSetFont (display, gc, font); 
  105.  
  106.     return (gc);
  107. }    
  108.     
  109. /*    
  110. ** Terminate the program gracefully    
  111. */    
  112. quitX ()    
  113. {
  114.     if (font != 0)
  115.         XUnloadFont (display, font);    
  116.     XCloseDisplay (display);    
  117.     exit (0);    
  118. }    
  119.     
  120.     
  121. /* 
  122. ** An expose event occurs when the contents of
  123. ** a window are invalidated and at least some
  124. ** of it needs to be redrawn 
  125. */ 
  126. void    
  127. doExposeEvent (pEvent)    
  128.     XExposeEvent *pEvent;    
  129. {    
  130.     XDrawImageString (display, main_window, gc,    
  131.           10, 10, "Press q or any button to quit",    
  132.           strlen ("Press q or any button to quit"));    
  133. }    
  134.     
  135. /* 
  136. ** A button has been pressed within the window - quit
  137. */ 
  138. void doButtonPressEvent (pEvent)    
  139.     XButtonEvent *pEvent;    
  140. {    
  141.     quitX ();    
  142. }    
  143.     
  144. /* 
  145. ** A key has been pressed. 
  146. ** It needs to be decoded and acted on.
  147. ** Quit if it is a 'q'.
  148. */ 
  149. void    
  150. doKeyPressEvent (pEvent)    
  151.     XKeyEvent *pEvent;    
  152. {   int key_buffer_size = 10;    
  153.     char key_buffer[9];    
  154.     XComposeStatus compose_status;    
  155.     KeySym key_sym;    
  156.     
  157.     XLookupString (pEvent, key_buffer, 
  158.           key_buffer_size,    
  159.           &key_sym, &compose_status);    
  160.     if (key_buffer[0] == 'q')    
  161.           quitX ();    
  162. }    
  163.     
  164. void
  165. initX ()    
  166. {   char **font_list;
  167.     int count;
  168.     
  169.     /* set the display name from the environment 
  170.        vbl DISPLAY */    
  171.     display = XOpenDisplay (NULL);    
  172.     if  (display == NULL)    
  173.     {      fprintf (stderr, 
  174.                     "Unable to open display %s\n",    
  175.                     XDisplayName (NULL));    
  176.           exit (1);    
  177.     }    
  178.     screen = DefaultScreen (display);    
  179.     /* use the default foreground and 
  180.     ** background colors 
  181.     */    
  182.     foreground = BlackPixel (display, screen);    
  183.     background = WhitePixel (display, screen);    
  184.     
  185.     font_list = XListFonts (display, FONT, 1, &count);
  186.     if (count > 0)
  187.     {   /* found the font we want, 
  188.            use the first match */
  189.         font = XLoadFont (display, font_list[0]);
  190.         XFreeFontNames (font_list);
  191.     }
  192.     else    
  193.     {   /* this font not on the server. set it to
  194.         ** a non-existent value
  195.          */
  196.         font = (Font) 0;
  197.         fprintf (stderr, "couldn't find font %s\n",
  198.                  FONT);
  199.     }
  200. }    
  201.     
  202. main (argc, argv)    
  203.     int argc;    
  204.     char **argv;    
  205. {   XEvent event;    
  206.     
  207.     initX ();    
  208.     
  209.     main_window = openWindow (10, 20, 200, 100, 5, 
  210.                               argc, argv);    
  211.     gc = getGC ();    
  212.     
  213.     /* Display the window on the screen */    
  214.     XMapWindow (display, main_window);    
  215.     XFlush (display);    
  216.     while (True)    
  217.     {    
  218.           XNextEvent (display, &event);    
  219. #ifdef DEBUG    
  220.           printf ("Event number is %d\n", event.type);
  221. #endif    
  222.           switch  (event.type)    
  223.           {    
  224.           case Expose:  
  225.                       doExposeEvent (&event);    
  226.                       break;    
  227.     
  228.           case ButtonPress:    
  229.                       doButtonPressEvent (&event);    
  230.                       break;    
  231.     
  232.           case KeyPress:
  233.                       doKeyPressEvent (&event);    
  234.                       break;    
  235.     
  236.           case MappingNotify:    
  237.                       XRefreshKeyboardMapping (&event);
  238.                       break;    
  239.           }    
  240.     }    
  241. }
  242.